<template>
    <rx-dialog @handOk="handleCompleteCheck" @cancel="closeWindow" :oktext="$ti18('rxForm.commitBtn','提交')" order="buttom" btnalign="center">
        <rx-fit>
            <div style="height:100%;width:100%;padding:20px;">
                <a-form-model>
                    <a-form-model-item :label="i18n('taskApply.selectedTask','已选择任务')" :label-col="labelCol" :wrapper-col="wrapperCol">
                        <rx-grid ref="table"
                                 style="min-height: 500px;"
                                 :dataSource="tasksInfo"
                                 :allowRowSelect="false"
                                 :multiSelect="true"
                                 :showPage="false"
                                 :columns="columns"
                        >
                             <span slot="nodeExecutors" slot-scope="{ text, record }">
                                    <bpm-task-executors
                                        ref="taskExecutors"
                                        :nodeExecutors="record.nodeExecutors"
                                        :allow-select-executor="record.allowSelectExecutor"
                                        :allow-select-group="record.allowSelectGroup"
                                        :task-config="record.taskConfig"
                                        :processConfig="record.processConfig">
                                    </bpm-task-executors>
                             </span>
                             <span slot="isBatch" slot-scope="{ text, record }">
                                <a-tag :color="text==='YES' ? 'green' : 'red'">
                                    <span v-if="text==='YES'">{{i18n('taskApply.permit','允许')}}</span>
                                    <span v-else-if="text==='NO'">{{i18n('taskApply.notAllow','不允许')}}</span>
                                </a-tag>
                            </span>
                            <span slot="action" slot-scope="{ text, record,index }" class="actionIcons">
                                <span @click.stop="del(index)">{{$ti18('rxList.delBtn','删除')}}</span>
                            </span>
                            <span slot="status" slot-scope="{ text, record }">
                                <a-tag :color="statusMap[text].color">
                                  <span :title="record.message">{{statusMap[text].text}}</span>
                                </a-tag>
                            </span>
                        </rx-grid>
                    </a-form-model-item>
                    <a-form-model-item :label="i18n('taskApply.applyAction','审批动作')" :label-col="labelCol" :wrapper-col="wrapperCol">
                        <a-radio-group v-model="form.checkType">
                            <a-radio value="AGREE">{{i18n('taskApply.agree','同意')}}</a-radio>
                            <a-radio value="BACK_TO_STARTOR">{{i18n('taskApply.reject','驳回到发起人')}}</a-radio>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item :label="i18n('taskApply.opinion','意见')" :label-col="labelCol" :wrapper-col="wrapperCol">
                        <a-textarea :placeholder="i18n('taskApply.opinion','请填写意见','write')" v-model="form.opinion" :rows="8"></a-textarea>
                    </a-form-model-item>
                </a-form-model>
            </div>
        </rx-fit>
    </rx-dialog>
</template>
<script>
import {Dialog,RxGrid, RxDialog, Util, RxFit} from '@lowcode/jpaas-common-lib';
import BpmtaskApi from '@/api/bpm/core/bpmTask';
import BpmTaskExecutors from "@/views/modules/bpm/core/BpmTaskExecutors.vue";

export default {
    name: 'bpm-task-check',
    components: {
        RxFit,
        RxDialog,
        RxGrid,
        BpmTaskExecutors
    },
    props: {
        layerid: String,
        lydata: Object,
        destroy: Function,
        taskIds: String
    },
    data() {
        return {
            labelCol: {span: 4},
            wrapperCol: {span: 8},
            form: {
                taskIds: this.taskIds,
                checkType: "AGREE",
                opinion: ""
            },
            columns:[
                {title: this.i18n('taskApply.name','标题'), dataIndex: 'subject', width: 70},
                {title: this.i18n('taskApply.approver','审批人'), dataIndex: 'nodeExecutors', width: 230,scopedSlots: {customRender: 'nodeExecutors'}},
                {title: this.i18n('taskApply.allowableBatch','允许批量'), dataIndex: 'isBatch', width: 40,scopedSlots: {customRender: 'isBatch'}},
                {title: this.$ti18('rxList.action','操作'), width: 30, dataIndex: 'action', scopedSlots: {customRender: 'action'}},
                {title: this.i18n('form.status','状态'), width: 30, dataIndex: 'status', scopedSlots: {customRender: 'status'}}
            ],
            //已选择任务详情
            tasksInfo: [],
            statusMap: {
                0: { color: 'blue', text: this.i18n('statusMap.notApproved','未审批') },
                1: { color: 'green', text: this.i18n('statusMap.approvedSuccess','审批成功') },
                2: { color: 'red', text: this.i18n('statusMap.approvedFailure','审批失败') },
            },
        }
    },
    created() {
        this.getTaskInfo();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"MyBpmTask",key);
        },
        //获取已选择的任务
        getTaskInfo() {
            BpmtaskApi.getTasksInfo(this.taskIds).then(res => {
                if (res && res.data) {
                    var tasks = [];
                    for (let i = 0; i < res.data.length; i++) {
                        if (res.data[i].bpmTask) {
                            tasks.push(res.data[i]);
                        }
                    }
                    this.initTaskInfo(tasks);
                }
            });
        },
        //初始化任务格式
        initTaskInfo(tasks) {
            for (let i = 0; i < tasks.length; i++) {
                var obj = {
                    taskId:tasks[i].bpmTask.pkId,
                    subject: tasks[i].bpmTask.subject,
                    nodeExecutors: tasks[i].nodeExecutors,
                    taskConfig: tasks[i].taskConfig,
                    status: "0",
                    message: this.i18n('statusMap.notApproved','未审批'),
                    processConfig:tasks[i].processConfig
                }
                //计算是否允许批量审批
                var processConfig = tasks[i].processConfig;
                var taskConfig = tasks[i].taskConfig;
                obj.allowSelectExecutor = taskConfig.switchOptions.includes("allowSelectExecutor");
                obj.allowSelectGroup = taskConfig.switchOptions.includes("allowSelectGroup");
                var rtn = processConfig.switchOptions.includes("notAllowBatch");
                if(!rtn){
                    rtn = taskConfig.switchOptions.includes("notAllowBatch");
                }
                //YES 允许 NO 不允许
                obj.isBatch =rtn?"NO":"YES";
                this.tasksInfo.push(obj);
            }
        },
        closeWindow() {
            Util.closeWindow(this, 'cancel')
        },
        handleCompleteCheck(vm) {
            var self_ = this;
            var taskIds=[];
            this.tasksInfo.find(item=>{
                //允许批量审批且未审批过
                if(item.isBatch=="YES" && item.status!="1"){
                    taskIds.push(item.taskId);
                }
            });
            if(taskIds.length==0){
                vm.loading=false;
                this.$message.warning(this.i18n('msa.unEnforceableTask','无可执行的任务!'));
                return;
            }
            self_.form.taskIds=taskIds.join(",");
            this.$confirm({
                title: this.$ti18('rxMsa.tipsInfo','提示信息'),
                content: this.i18n('msa.confirmApplyTips','确认审批吗?'),
                okText: this.$ti18('rxForm.ackBtn','确认'),
                cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                zIndex: 30000,
                onOk() {
                    BpmtaskApi.batApprove(self_.form).then(res => {
                        if(res &&  res.data){
                            for (let i = 0; i <res.data.length ; i++) {
                                var data= res.data[i];
                                self_.tasksInfo.find(item=>{
                                    if(item.taskId==data.taskId){
                                        item.status=data.result.success?"1":"2";
                                        item.message=data.result.message;
                                    }
                                });
                            }

                            let status=res.code;
                            let message=res.message;
                            switch (status){
                                case 0:
                                    self_.$message.success(message);
                                    break;
                                case -1:
                                    self_.$message.error(message);
                                    break;
                                case 1:
                                    self_.$message.warn(message);
                                    break;
                            }
                            Util.closeWindow(self_,"ok");
                        }
                        vm.loading=false;
                    })
                },
                onCancel(){
                    vm.loading=false;
                }
            });

        },
        del(index){
            this.tasksInfo.splice(index,1);
        }
    }
}
</script>